function [nmodel,dat]=i_mooring(dat,model,parameter)

% -----------------------------------------------------------
% -- Interpolating modelled fields on observation's position
% -- in the "mooring" case
% -----------------------------------------------------------
% G. Charria (10/2008)

% ---- Create the Structure ...
nmodel = struct('dummy',1);
    

% ---- Control that the model is covering the same period as the data
if (min(model.time) > max(max(dat.timedata))) || (max(model.time) < min(min(dat.timedata)))
    errordlg('The model is not covering the same period as data. Please, try to use a different year')
    return
else
    % ----- 0. transform data from 1D to 2D (time,depth)
%      % scan existing different depths
%      zz=unique(dat.depthdata(~isnan(dat.depthdata)));
%      
%      % scan existing different time
%      tt=unique(dat.timedata( (dat.timedata >= min(model.time)) & (dat.timedata <= max(model.time))));     
%      
   

    %ndata=dat.data;
    nlon=dat.londata;
    nlat=dat.latdata;
    tt=dat.timedata( (dat.timedata >= min(model.time)) & (dat.timedata <= max(model.time)));
    tt=unique(tt(~isnan(tt)));
    zz=unique(dat.depthdata(~isnan(dat.depthdata)));
    
    if strcmp(parameter,'T') || strcmp(parameter,'S')
        % create new matrix data
        %tic
        ndata(1:length(tt),1:length(zz))=NaN;
        %nlon(1:length(tt),1:length(zz))=NaN;
        %nlat(1:length(tt),1:length(zz))=NaN;
        h=waitbar(0,'Reshape matrix data for the chosen year ...');
        for ij=1:size(dat.data,2)
            waitbar(ij/size(dat.data,2))
            for ii=1:size(dat.data,1)
                %waitbar(ii/length(dat.data))
                it=find(tt == dat.timedata(ii,ij));
                iz=find(zz == dat.depthdata(ii,ij));
                if (~isempty(it) && ~isempty(iz))
                    ndata(it,iz)=dat.data(ii,ij);
                    %nlon(it,iz)=dat.londata(ii);
                    %nlat(it,iz)=dat.latdata(ii);
                end
            end
        end
        close(h)

    elseif strcmp(parameter,'NO3') || strcmp(parameter,'CHL') || strcmp(parameter,'PON') ||...
            strcmp(parameter,'DON')
        % create new matrix data
        %tic
        ndata(1:length(tt),1:length(zz))=NaN;
        h=waitbar(0,'Reshape matrix data for the chosen year ...');
        for ij=1:length(dat.data)
            waitbar(ij/length(dat.data))
            it=find(tt == dat.timedata(ij));
            iz=find(zz == dat.depthdata(ij));
            if (~isempty(it) && ~isempty(iz))
                ndata(it,iz)=dat.data(ij);                            
            end
        end
        close(h)                        
    end
    %toc

     
    % ---- I. Interpolation at the mooring position (lon,lat) ----
    % ---- Interpolation    
    d1(1:size(model.data,4),1:size(model.data,1))=NaN;       
    %d2(1:length(tt),1:size(model.data,1))=NaN;
    d2(1:size(model.data,4),1:size(model.data,1))=NaN;
    
    h=waitbar(0,'Horizontal and temporal interpolation ...');
    for iz=1:size(model.data,1)
        for it=1:size(model.data,4)
            waitbar(   ((iz-1)*(size(model.data,4)) + it)/((size(model.data,4))*size(model.data,1)),h)
            d1(it,iz)=griddata(model.lon,model.lat,squeeze(model.data(iz,:,:,it)),nanmedian(nanmedian(nlon)),nanmedian(nanmedian(nlat)));
            % ---
            %figure
            %subplot(1,2,1)
            %pcolor(model.lon,model.lat,squeeze(model.data(iz,:,:,it))); shading flat; colorbar;
            %hold on
            %plot(nanmedian(nanmedian(nlon)),nanmedian(nanmedian(nlat)),'w+')
            %subplot(1,2,2)
            %plot(nanmedian(nanmedian(nlat)),d1(it,iz),'+')
            %hold on
            %matdist=distgrid(model.lon,model.lat,nanmedian(nanmedian(nlon)),nanmedian(nanmedian(nlat)));
            %[I,J]=find(matdist==min(min(matdist)));
            %plot(nanmedian(nanmedian(nlat)),squeeze(model.data(iz,I(1),J(1),it)),'ro')
            %pause
        end
        
        %whos
        
        % ---- II. Interpolation over the data time (time) ----
        %d2(:,iz)=interp1(model.time,squeeze(d1(:,iz)),tt);
        % ---
        %figure
        %plot(tt,squeeze(d2(:,iz)),'+')
        %hold on
        %plot(model.time,squeeze(d1(:,iz)),'or')
        %pause     
        
        % ---- II. No interpolation over the data time (time) ----
        d2(:,iz)=d1(:,iz);
        
    end
    close(h)        
      
    % ---- III. Interpolation over the cruise depths ----
    h=waitbar(0,'Vertical interpolation ...');    
    d4(1:length(tt),1:length(zz))=NaN;
    for it=1:length(tt)
        waitbar(it/length(tt),h)  
        d4(it,:)=interp1(model.depth,squeeze(d2(it,:)),zz);
        % ---
        %figure
        %plot(zz,squeeze(d4(it,:)),'+')
        %hold on
        %plot(model.depth,squeeze(d2(it,:)),'or')
        %pause
    end
    close(h)
            
    nmodel.lon=nlon;
    nmodel.lat=nlat;
    nmodel.data=d4;
    nmodel.depth=zz;
    nmodel.time=tt;
    
    dat.londata=nlon;
    dat.latdata=nlat;
    dat.data=ndata;
    dat.depthdata=zz;
    dat.timedata=tt;
    
    
    
end


% ---- Clean the Structure ...
nmodel=rmfield(nmodel,'dummy');    
